<template>
  <div>
<!--    <a-row>-->
<!--      <a-col :span="17">-->
<!--        <a-form-item :wrapper-col="{ span: 16 }" label="选择导入的Excel文件" :labelCol="{ span:6 }">-->
<!--          <a-upload-->
<!--              :file-list="fileList"-->
<!--              :multiple="false"-->
<!--              :before-upload="beforeUpload"-->
<!--              :remove="handleRemove"-->
<!--              accept=".xlsx,.xls"-->
<!--          >-->
<!--            <a-button class="margin-left-10 buttonStyle"> <a-icon type="upload" />点击选择需要导入的Excel文件</a-button>-->
<!--          </a-upload>-->
<!--        </a-form-item>-->
<!--      </a-col>-->
<!--      <a-col :span="7" class="text-align-right">-->
<!--        <a-form-item>-->
<!--          <a-button  icon="download" @click="download" class="buttonStyle">-->
<!--            下载模板文件-->
<!--          </a-button>-->
<!--        </a-form-item>-->
<!--        <a-form-model-item class="text-align-right" style="margin-top: 50px">-->
<!--          <a-button  @click="mClose" class="margin-right-10">-->
<!--            关闭-->
<!--          </a-button>-->
<!--          <a-button type="link"  html-type="submit"  @click="handleUpload">-->
<!--            导入-->
<!--          </a-button>-->
<!--        </a-form-model-item>-->
<!--      </a-col>-->
<!--    </a-row>-->

    <a-row>
      <a-col :span="24">
        <a-form-item :wrapper-col="{ span: 17 }" label="文件上传" :labelCol="{ span:4 }">
          <a-upload-dragger
            :file-list="fileList"
            :multiple="false"
            :before-upload="beforeUpload"
            :remove="handleRemove"
            accept=".xlsx,.xls"
          >
            <p class="ant-upload-drag-icon">
              <a-icon type="inbox"/>
            </p>
            <p class="ant-upload-text">
              点击或将文件拖拽到这里上传
            </p>
            <p class="ant-upload-hint">
              支持扩展名：.xlsx .xls
            </p>
          </a-upload-dragger>
        </a-form-item>

        <a-form-model-item class="text-align-right" style="margin-top: -20px; margin-right: 75px;">
          <a-button type="link" html-type="submit" @click="download">
            下载模板
          </a-button>
        </a-form-model-item>

        <a-form-model-item class="text-align-center" style="margin-top: 40px;">
          <a-button type="primary" html-type="submit" @click="handleUpload" class="margin-right-20">
            导入
          </a-button>
          <a-button @click="mClose">
            关闭
          </a-button>
        </a-form-model-item>
      </a-col>
    </a-row>

  </div>
</template>

<script>
export default {
  name: "导入功能",
  props: {
    mUrl:String,
    mHref:String,
    assetType:String,
    enterpriseType:String
  },
  data(){
    return {
      fileList: [],
    }
  },
  mounted() {
  },
  methods:{
    handleRemove(file) {
      const index = this.fileList.indexOf(file);
      const newFileList = this.fileList.slice();
      newFileList.splice(index, 1);
      this.fileList = newFileList;
    },
    download(){
      console.log(111,this.mHref);
      window.location.href=this.mHref;
    },
    beforeUpload(file) {
      this.fileList = [file];
      return false;
    },
    mClose() {//关闭功能
      this.$emit('modalClose',{refresh: false})
    },
    handleUpload(){
      let self = this;
      if(this.fileList.length==0){
        self.$message.error('请导入模板文件');
        return;
      }
      const { fileList } = this;
      const formData = new FormData();
      fileList.forEach(file => {
        formData.append('excelFile', file);
      });
      if(self.assetType){
        formData.append('assetTypeId', self.assetType);
      }
      if(self.enterpriseType){
        formData.append('type', self.enterpriseType);
      }

      if(this.mUrl == 'batchImportQuestion'){
        formData.append('objectType', (self.$route.query&&self.$route.query.objectType)?self.$route.query.objectType:1);
      }

      this.$util.post(this.mUrl,formData).then((res)=>{
        if(res.content){
          self.$message.error(res.errorMessage);
          window.location.href=res.content;
        }else{
          self.$message.success('导入成功');
        }
        self.$emit('modalClose',{refresh:true});
      })


    }
  }
}
</script>

<style scoped>
.buttonStyle{height: 40px;padding: 0 60px}
</style>
